module.exports = function(e) {
	// 公用模块用法请参考 https://uniapp.dcloud.io/uniCloud/cf-common
	return e
}

const { mallSpaceId } = require('config-main-space');

async function checkToken(uniIdToken, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}
	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});
	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/checkToken',
			data: {
				uniIdToken: uniIdToken
			}
		}
	});
	return callFunctionResult.result;
}

async function encryptPwd(password, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/encryptPwd',
			data: {
				password: password
			}
		}
	});

	return callFunctionResult.result;
}

async function resetPwd(uid, password, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/resetPwd',
			data: {
				uid: uid,
				password: password
			}
		}
	});

	return callFunctionResult.result;
}

async function setVerifyCode(mobile, code, expiresIn, type, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/setVerifyCode',
			data: {
				mobile: mobile,
				code: code,
				expiresIn: expiresIn,
				type: type
			}
		}
	});

	return callFunctionResult.result;
}

async function verifyCode(mobile, code, type, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/verifyCode',
			data: {
				mobile: mobile,
				code: code,
				type: type
			}
		}
	});

	return callFunctionResult.result;
}

async function bindMobile(uid, mobile, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/bindMobile',
			data: {
				uid: uid,
				mobile: mobile
			}
		}
	});

	return callFunctionResult.result;
}

async function bindMobileBySms(uid, mobile, code, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/bindMobileBySms',
			data: {
				uid: uid,
				mobile: mobile,
				code: code
			}
		}
	});

	return callFunctionResult.result;
}

async function register(username, password, nickname, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/register',
			data: {
				username: username,
				password: password,
				nickname: nickname
			}
		}
	});

	return callFunctionResult.result;
}

async function login(username, email, mobile, password, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/login',
			data: {
				username: username,
				email: email,
				mobile: mobile,
				password: password
			}
		}
	});

	return callFunctionResult.result;
}

async function logout(uniIdToken, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/logout',
			data: {
				uniIdToken: uniIdToken
			}
		}
	});

	return callFunctionResult.result;
}

async function loginBySms(mobile, code, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/loginBySms',
			data: {
				mobile: mobile,
				code: code
			}
		}
	});

	return callFunctionResult.result;
}

async function getUserInfo(uid, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/getUserInfo',
			data: {
				uid: uid
			}
		}
	});

	return callFunctionResult.result;
}

async function setUserInviteCode(uid, myInviteCode, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/setUserInviteCode',
			data: {
				uid: uid,
				myInviteCode: myInviteCode
			}
		}
	});

	return callFunctionResult.result;
}

async function getUserInfoByCode(uid, myInviteCode, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/getUserInfoByCode',
			data: {
				uid: uid,
				myInviteCode: myInviteCode
			}
		}
	});

	return callFunctionResult.result;
}

async function getInvitedUser(uid, limit, offset, needTotal, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/getInvitedUser',
			data: {
				uid: uid,
				limit: limit,
				offset: offset,
				needTotal: needTotal
			}
		}
	});

	return callFunctionResult.result;
}

async function updatePwd(uid, oldPassword, newPassword, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/updatePwd',
			data: {
				uid: uid,
				oldPassword: oldPassword,
				newPassword: newPassword
			}
		}
	});

	return callFunctionResult.result;
}

async function registerAdmin(username, password, role, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/registerAdmin',
			data: {
				username: username,
				password: password,
				role: role
			}
		}
	});

	return callFunctionResult.result;
}

async function registerUser(data, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/registerUser',
			data: data
		}
	});

	return callFunctionResult.result;
}

async function setAvatar(uid, avatar, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/setAvatar',
			data: {
				uid: uid,
				avatar: avatar
			}
		}
	});

	return callFunctionResult.result;
}

async function updateUser(data, spaceId) {
	if(!spaceId){
		spaceId = mallSpaceId;
	}

	const mainCloud = uniCloud.init({
		provider: 'tencent',
		spaceId: spaceId
	});

	let callFunctionResult = await mainCloud.callFunction({
		name: "mall-uni-id",
		data: {
			path: '/uniId/updateUser',
			data: data
		}
	});

	return callFunctionResult.result;
}

module.exports = {
	checkToken,
	encryptPwd,
	resetPwd,
	setVerifyCode,
	verifyCode,
	bindMobile,
	bindMobileBySms,
	register,
	login,
	logout,
	loginBySms,
	getUserInfo,
	setUserInviteCode,
	getUserInfoByCode,
	getInvitedUser,
	updatePwd,
	registerAdmin,
	registerUser,
	setAvatar,
	updateUser
}
